package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.Calendar;

import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextPane;
import javax.swing.ScrollPaneConstants;
import javax.swing.border.LineBorder;
import javax.swing.text.BadLocationException;
import javax.swing.text.Style;
import javax.swing.text.StyleConstants;
import javax.swing.text.StyleContext;
import javax.swing.text.StyledDocument;

import core.Utility;

import data.Event;
import data.EventDateTime;

public class EventNotePanel  extends JPanel implements KeyListener
{
	private Dimension noteSize = new Dimension(420, 49);
	private JTextPane noteBox;
	private JScrollPane scrollPane;
	private JDialog dialog;
	public EventNotePanel(Frame window) {
		this.setSize(noteSize);
		noteBox = new JTextPane();
		noteBox.setSize(noteSize);
		noteBox.setEditable(false);
		scrollPane = new JScrollPane(noteBox);
		scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
		scrollPane.setBorder(new LineBorder(new Color(0, 0, 139), 2, true));
		dialog = new JDialog(window);
		dialog.setResizable(false);
		dialog.setContentPane(this);
		dialog.addKeyListener(this);
		dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		dialog.setUndecorated(true);
		dialog.setSize(noteSize);
		setLayout(null);
		scrollPane.setSize(noteSize);
		//eventDetail.setHorizontalTextPosition(JLabel.LEFT);
		//this.setLayout(null);
		//scrollPane.add(noteBox);
		this.add(scrollPane);
		//scrollPane.setC
	}

	@Override
	public void keyTyped(KeyEvent e) {}

	@Override
	public void keyReleased(KeyEvent e) {}

	@Override
	public void keyPressed(KeyEvent e) {
		if(e.getKeyCode() == KeyEvent.VK_ESCAPE) {
			//noteBox.setVisible(false);		
			//noteBox.setVisible(false);
			dialog.setVisible(false);
		}
	}

	@Override
	public void setVisible(boolean value) {
		super.setVisible(value);
		dialog.setVisible(value);
		dialog.requestFocus();
		scrollPane.setVisible(value);
		if (value) {
			dialog.toFront();
		}
	}
	
	public ArrayList<String> toString(Event event) {
		ArrayList<String> result = new ArrayList<String>();
		if (event.getName() != null) {
			result.add(event.getName());
		}
		EventDateTime defaultDateTime = new EventDateTime();
		if (!(event.getStart().getTime().equals(defaultDateTime))) {
			int hourTime;
			int minTime;
			String time = "";
			hourTime = event.getStart().get(Calendar.HOUR);
			minTime = event.getStart().get(Calendar.MINUTE);

			String suffix = event.getStart().getTime().getDisplayName(Calendar.AM_PM, Calendar.SHORT).toLowerCase();
			time+=hourTime;
            if (minTime!=0) {
            	time+="."+minTime;
            }
            result.add(" at");
            result.add(" "+time+suffix);   
		}
		
		if(!(event.getStart().getDate().equals(defaultDateTime))) {
			int day = event.getStart().get(Calendar.DAY_OF_MONTH);
			String month=event.getStart().getDate().getDisplayName(Calendar.MONTH,Calendar.SHORT);
			
			if (event.getDeadlineEvent()) {
				result.add(" by");
			} else {
				result.add(" on");
			}
            result.add(" "+day + Utility.getDaySuffix(day) + " " + month) ;
			
			
			int year = event.getStart().getDate().get(Calendar.YEAR);
			if (year != EventDateTime.current().get(Calendar.YEAR)) {
				String year1="";
				year1+=year;
				result.add(" "+year1);
			}
		}
		if (event.getLabels() != null) {
			String label ="";
			for(int i=0;i<event.getLabels().size();i++) {
				label +=event.getLabels().get(i);
			}
			result.add("@");
			result.add(" "+label);
		}
		return result;
	}
	

	@Override
	public void setLocation(int x, int y) {
		dialog.setLocation(x,y);
	}

	public void setMessage(Event e) {
	
		//noteBox.setText();
		ArrayList<String> resultFormat = toString(e);
		noteBox.setText("");
		createStyle(resultFormat);
	}
	
	private void createStyle(ArrayList<String> resultFormat) {
		StyledDocument doc = noteBox.getStyledDocument();
        addStylesToDocument(doc);
     
        String[] initStyles =
                { "bold","regular", "bold", "regular","bold", "bold","regular", "bold"};
        try {
            for (int i = 0; i < resultFormat.size(); i++) {
            	
                doc.insertString(doc.getLength(), resultFormat.get(i),
                                 doc.getStyle(initStyles[i]));
            }
        } catch (BadLocationException ble) {
            System.err.println("Couldn't insert initial text into text pane.");
        }
    }

    protected void addStylesToDocument(StyledDocument doc) {
        Style def = StyleContext.getDefaultStyleContext().
                        getStyle(StyleContext.DEFAULT_STYLE);

        Style regular = doc.addStyle("regular", def);
        StyleConstants.setFontFamily(def, "Tempus Sans ITC");
        StyleConstants.setForeground(def, Color.RED);
        StyleConstants.setFontSize(def, 14);

        Style s = doc.addStyle("italic", regular);
        StyleConstants.setItalic(s, true);

        s = doc.addStyle("bold", regular);
        StyleConstants.setBold(s, true);
        StyleConstants.setForeground(s, Color.blue);

        s = doc.addStyle("small", regular);
        StyleConstants.setFontSize(s, 10);

        s = doc.addStyle("large", regular);
        StyleConstants.setFontSize(s, 16);
    }

}
